package com.auxgroup.smarthome.business.util;

import com.auxgroup.smarthome.utils.excel.Excel;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.formula.functions.T;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: laiqiuhua.
 * @Date: 2017/9/25 19:12.
 */
public class DownloadForResponseUtil {
    private DownloadForResponseUtil(){}

    public static void downloadExcel(Workbook workBook, String fileName,
                               HttpServletResponse response) throws IOException {
        response.setContentType("application/msexcel;");
        if (workBook instanceof HSSFWorkbook) {
            response.setHeader("Content-Disposition", "attachment;filename="
                    + URLEncoder.encode(fileName, "UTF-8") + ".xls");
        } else if (workBook instanceof XSSFWorkbook) {
            response.setHeader("Content-Disposition", "attachment;filename="
                    + URLEncoder.encode(fileName, "UTF-8") + ".xlsx");
        } else {
            System.err.print("不支持的office版本");
            return;
        }
        OutputStream os = null;
        try {
            os = response.getOutputStream();
            workBook.write(os);
            os.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                os.close();
            }
            if (null != workBook) {
                workBook.close();
            }
        }

    }

    /**
     *
     * @param titleList
     * @param clazz
     * @return
     * @throws NoSuchFieldException
     * @throws NoSuchMethodException
     */
    public static List<Method> getExportFiledMethod(List<String> titleList, Class<?> clazz) throws NoSuchFieldException, NoSuchMethodException {
        List<Method> invokeMethod = new ArrayList<>();
        for (int j = 0; j < titleList.size(); j++) {
            String title = titleList.get(j);
            Field field = clazz.getDeclaredField(title);
            StringBuilder sb = new StringBuilder();
            String name = field.getName();
            name = name.replaceFirst(name.substring(0, 1), name.substring(0, 1).toUpperCase());
            sb.append("get").append(name);
            Method method = clazz.getDeclaredMethod(sb.toString());
            invokeMethod.add(method);
        }
        return invokeMethod;
    }

    /**
     * 设置Excel导出数据
     * @param excel
     * @param sourceData
     * @param titleList
     * @param clazz
     * @param <T>
     * @throws NoSuchFieldException
     * @throws NoSuchMethodException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    public static <T> void setExcelData(Excel excel, List<T> sourceData, List<String> titleList, Class<?> clazz) throws NoSuchFieldException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        List<Method> invokeMethod = getExportFiledMethod(titleList, clazz);
        for (int i = 0; i < sourceData.size(); i++) {
            T data = sourceData.get(i);
            for (int j = 0; j < invokeMethod.size(); j++) {
                Object object = invokeMethod.get(j).invoke(data);
                excel.cell(i + 1, j).value(object);
            }
        }
    }

}
